package com.lujieni.nio.c1;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 遍历文件树
 */
public class TestWalkFileTree {

    public static void main(String[] args) throws IOException {

        m3();
    }

    // 删库跑路代码 哈哈
    private static void m3() throws IOException {

        AtomicInteger ai = new AtomicInteger();

        Files.walkFileTree(Paths.get("D:\\netty-demo"), new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });

        System.out.println(ai);

    }


    private static void m2() throws IOException {

        AtomicInteger ai = new AtomicInteger();

        Files.walkFileTree(Paths.get("D:/Java/jdk1.8.0_181"), new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // tuString()一定要加
                if(file.getFileName().toString().endsWith("jar")){
                    ai.incrementAndGet();
                }
                return super.visitFile(file, attrs);
            }
        });

        System.out.println(ai);

    }


    private static void m1() throws IOException {
        AtomicInteger dCount = new AtomicInteger();

        AtomicInteger fCount = new AtomicInteger();

        Files.walkFileTree(Paths.get("D:/Java/jdk1.8.0_181"), new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println(dir);
                dCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file);
                fCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }

        });

        System.out.println(dCount+"  " +fCount);
    }


}
